3 # bats_test_state_dump.sh
6 # Copyright (c) 2019 Apple Inc. All rights reserved.
10 function triger_state_dump
{
11 local command_line
="$1"
12 output
="$($command_line)"
13 if [[ $?
-ne 0 ]]; then
14 printf "dns-sd -O exit with non-zero return value, the returned error message is:\n"
21 # trigger state dump and check if the file is created successfully
22 function triger_state_dump_and_check_file
{
23 # $1 is the command line used to trigger state dump
24 triger_state_dump
"$1"
25 if [[ $?
-ne 0 ]]; then
28 # the returned result is like the following:
29 # State Dump Is Saved to: /private/var/log/mDNSResponder/mDNSResponder_state_dump_2019-03-01_17-07-10-260-08-00.txt
31 # splited the result with '\n'
32 IFS
=$
'\n' read -r -d '' -a line_being_read
<<< "$output"
33 # ${line_being_read[0]} contains "State Dump Is Saved to: /private/var/log/mDNSResponder/mDNSResponder_state_dump_2019-03-01_17-07-10-260-08-00.txt"
34 IFS
=":" read -r -d '' -a line_being_splitted
<<< "${line_being_read[0]}"
35 # get the path "/private/var/log/mDNSResponder/mDNSResponder_state_dump_2019-03-01_17-07-10-260-08-00.txt"
36 file_name
=$(echo "${line_being_splitted[1]}" | xargs)
37 # check if the file exists in the disk
38 if [ ! -f $file_name ]; then
39 printf "State dump is not created under %s\n" $file_name
45 # verify that the state dump contains the full content
46 function verify_state_dump_content
{
47 # the passed parameter is the file content to be checked
49 # the first line of file should start with "---- BEGIN STATE LOG ----"
50 local file_start_string
="---- BEGIN STATE LOG ----"
51 if [[ ! "$file" == "$file_start_string"* ]]; then
52 printf "State dump file does not start with %s\n" "$file_start_string"
56 # the last line of file should start with "---- END STATE LOG ----"
57 local last_line
=$(echo "$file" | tail -n1)
58 local file_end_string
="---- END STATE LOG ----"
59 if [[ ! "$last_line" == "$file_end_string"* ]]; then
60 printf "State dump file does not end with %s\n" "$file_end_string"
67 function test_check_dump_directory
{
68 local dump_path
="/private/var/log/mDNSResponder"
69 if [ ! -d "$dump_path" ]; then
70 printf "Directory \"%s\" does not exist\n" $dump_path
73 local permission
=$(stat -f "%OLp" $dump_path)
74 if [ ! $permission == "755" ]; then
75 printf "Directory \"%s\" has incorrect permission. expected=755; actual=%s\n" $dump_path $permission
78 local owner
=$(ls -ld $dump_path | awk '{print $3}')
79 if [ ! $owner == "_mdnsresponder" ]; then
80 printf "Directory \"%s\" has incorrect owner. expected=_mdnsresponder; actual=%s\n" $dump_path "$owner"
86 function test_output_to_plain_txt_file
{
87 triger_state_dump_and_check_file
"dns-sd -O"
88 if [[ $?
-ne 0 ]]; then
91 # get the file content
92 local file_content
=$(cat $file_name)
93 verify_state_dump_content
"$file_content" && rm "$file_name"
97 # tests if "dns-sd -O -compress" works as expected
98 function test_output_to_archive
{
99 triger_state_dump_and_check_file
"dns-sd -O -compress"
100 if [[ $?
-ne 0 ]]; then
103 # get the uncompressed file name
104 local file_name_uncompressed
=$(tar -tf "$file_name")
106 tar -xf "$file_name" --directory /tmp
/
107 # get the file content
108 local file_content
=$(cat "/tmp/$file_name_uncompressed")
109 verify_state_dump_content
"$file_content" && rm "$file_name" && rm "/tmp/$file_name_uncompressed"
113 # tests if "dns-sd -O -stdout" works as expected
114 function test_output_to_stdout
{
115 triger_state_dump
"dns-sd -O -stdout"
116 if [[ $?
-ne 0 ]]; then
119 # delete the last line of output, which is " Time Used: <time> ms"
120 output
=$(echo "$output" | sed '$d')
121 verify_state_dump_content
"$output"
125 # tests whether the state dump will create at most MAX_NUM_DUMP_FILES, to avoid wasting too much space.
126 function test_dump_limit
{
127 # calls "dns-sd -O -compress" for 10 times
129 while [ $counter -le 10 ]
131 triger_state_dump_and_check_file
"dns-sd -O -compress"
132 if [[ $?
-ne 0 ]];then
138 # $file_name is already initialized when we call "dns-sd -O -compress" above
139 local directory
=$(dirname "$file_name")
140 # get the number of files under $directory
141 local file_count
=$(ls -Uba1 "$directory" | grep ^mDNSResponder_state_dump_ | wc -l | xargs)
142 # clean up the directory
143 rm -rf ${directory}/*
144 # the number of files should be MAX_NUM_DUMP_FILES, which is defined as 5 in mDNSResponder
145 if [[ $file_count -eq 5 ]]; then
153 # Functions are put inside an array, use ($test) to evaluate it
154 declare -a tests
=("test_check_dump_directory"
155 "test_output_to_plain_txt_file"
156 "test_output_to_archive"
157 "test_output_to_stdout"
160 echo "----State Dump Test Start, $(date)----"
161 for test in "${tests[@]}"; do
162 echo "running $test:"
164 if [[ $?
-eq 0 ]]; then
165 echo "passed"$
'\n' # use $'\n' to print one more newline character
171 echo "----State Dump Test End, $(date)----"